/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.tenant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.market.base.tenant.mapper.ITenantQuotaMapper;
import net.siufung.market.base.tenant.mapper.entity.TenantQuota;
import net.siufung.market.base.tenant.service.ITenantQuotaService;
import net.siufung.mybatis.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统租户配额表 数据服务实现
 *
 * @author 陈建峰
 * @since 2022-07-06 14:45:08
 */
@Slf4j
@AllArgsConstructor
@Service(value = "tenantQuotaService")
@Transactional(rollbackFor = Exception.class)
public class TenantQuotaServiceImpl extends BaseServiceImpl<ITenantQuotaMapper, TenantQuota> implements ITenantQuotaService {

    @Override
    public boolean checkTenantQuotaKeyNotRepeat(VerifyRepeatDto<Integer> verifyRepeatDto) {
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0L == getBaseMapper().selectCount(new LambdaQueryWrapper<TenantQuota>()
                    .eq(TenantQuota::getQuotaKey, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0L == getBaseMapper().selectCount(new LambdaQueryWrapper<TenantQuota>()
                    .eq(TenantQuota::getQuotaKey, verifyRepeatDto.getVerifyKeyValue())
                    .ne(TenantQuota::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }
}